package com.ycu.jk16.mall.dao;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.SHIPPINGTABLE;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.mapping.FetchType;

import com.ycu.jk16.mall.dao.provider.ShippingDynaSqlProvider;
import com.ycu.jk16.mall.domain.Shipping;

/**
 * 
 * @ClassName: ShippingDao
 * @Description: TODO(Shipping表的查询)
 * @author 42
 * @date 2018年12月11日 下午6:56:15
 * 
 * @author 倪宗龙
 * @version 2
 * @@Description: 新增@Results匹对数据库字段user_id
 */

public interface ShippingDao {
	// 根据id返回地址
	@Select("select * from " + SHIPPINGTABLE + " where id = #{id}")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)), })
	Shipping selectById(@Param("id") Integer id);

	// 返回地址列表
	@SelectProvider(type = ShippingDynaSqlProvider.class, method = "selectWithParam")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)), })
	List<Shipping> selectShippingWithParam(Map<String, Object> params);

	// 根据用户id查找地址总数
	@Select("select count(*) from " + SHIPPINGTABLE + " where user_id = #{userId}")
	Integer countShipping(Integer userId);

	// 动态修改地址信息
	@UpdateProvider(type = ShippingDynaSqlProvider.class, method = "updateShipping")
	void updateShipping(Shipping shipping);

	// 添加地址信息
	@InsertProvider(type = ShippingDynaSqlProvider.class, method = "insertShipping")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 插入后返回自增Id
	void insertShipping(Shipping shipping);

	// 删除地址信息
	@Delete("delete from " + SHIPPINGTABLE + " where id = #{id}")
	void deleteShipping(@Param("id") Integer id);
}
